package org.example.backend.Module.School.Course.Service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.backend.Module.School.Course.Mapper.CourseMapper;
import org.example.backend.Module.School.Course.Pojo.Entity.Course;
import org.example.backend.Module.School.Course.Service.CourseService;
import org.example.backend.Module.System.User.Pojo.Entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Override
    public List<Course> list(String name, Boolean enabled) {
        LambdaQueryWrapper<Course> qw = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)) {
            qw.like(Course::getName, name);
        }

        if(enabled != null) {
            qw.eq(Course::getEnabled, enabled);
        }

        return courseMapper.selectList(qw);
    }

    @Override
    public void addOrUpdate(Course course, User creator) {
        Date now = new Date();

        course.setUpdateTime(now);

        if(course.getId() == null) {
            course.setCreator(creator.getId());
            course.setCreateTime(now);
        }

        courseMapper.insertOrUpdate(course);
    }

    @Override
    public void delete(Long id) {
        courseMapper.deleteById(id);
    }
}
